<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Merchant extends Model
{
    use HasFactory, SoftDeletes;

    const NOTIFY_TYPE_NORMAL = 'normal';

    const NOTIFY_TYPE_EPAY = 'epay';

    public static $notifyTypeMap = [
        self::NOTIFY_TYPE_NORMAL => '正常',
        self::NOTIFY_TYPE_EPAY => '易支付',
    ];

    protected $fillable = [
        'user_id', 'client_id', 'client_secret', 'notify_type', 'balance', 'frozen_balance'
    ];

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        self::creating(function ($model) {
            if (empty($model->client_id)) {
                $model->client_id = self::query()->count() + 150000;
            }
        });
    }

    public static function getMercantByClientId($clientId)
    {
        return self::query()->where('client_id', $clientId)->firstOrFail();
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}
